<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面</title>
</head>
<body>
<h2 id="welcome"></h2>
<button id="logoutBtn">退出登录</button>

<script>
    async function checkLogin() {
        const response = await fetch("checkLogin", {
            method: "GET",
            credentials: "include"  // ✅ 同样要带上 session cookie
        });
        const data = await response.json();

        const welcome = document.getElementById("welcome");

        if (data.loggedIn) {
            welcome.innerText = `欢迎你，${data.username}`;
        } else {
            alert("请先登录！");
            window.location.href = "login.html";
        }
    }

    document.getElementById("logoutBtn").addEventListener("click", async () => {
        await fetch("logout", { method: "GET", credentials: "include" });
        alert("已退出登录");
        window.location.href = "login.html";
    });

    checkLogin();
</script>
</body>
</html>
